﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SIGEB.Prototipo.Control;
using SIGEB.Modelo.moduloEstadisticas;
using System.Drawing.Imaging;

namespace SIGEB.Prototipo.Vista.moduloEstadisticas
{
    public partial class frm_ReporteEstadistico : frm_Base
    {
        #region CONSTRUCTORES

        public frm_ReporteEstadistico()
        {
            InitializeComponent();
        }

        public frm_ReporteEstadistico(controlPrincipal cp):base(cp)
        {
            InitializeComponent();
            asignarUI(this.Controls);

            coleccionDatos = new DataCollection();
            PlotPanel.Paint += new PaintEventHandler(PlotPanel_Paint);
            estiloBarra = new ChartStyle(this);

            mm = new MainMenu();
            m1 = new MenuItem("Reporte");
            m2 = new MenuItem("Imprimir", new EventHandler(m2_Click));
            m3 = new MenuItem("Salir", new EventHandler(m3_Click), Shortcut.AltF4);
            m1.MenuItems.Add(m2);
            m1.MenuItems.Add("-");
            m1.MenuItems.Add(m3);
            mm.MenuItems.Add(m1);
            Menu = mm;
        }

        #endregion

        #region MÉTODOS

        private void m2_Click(object sender, EventArgs e)
        {
            //PrintPreviewDialog pp = new PrintPreviewDialog();
            //pp.ShowIcon = false;
            //pp.Document = printDocument1;
            //pp.Show();
            object[] param = new object[6];
            param[0] = this.estiloBarra;
            param[1] = this.coleccionDatos;
            param[2] = this.seriesDatos;
            param[3] = this.datos;
            param[4] = new Size(662, 680); //661, 500
            param[5] = this.PlotPanel;
            cp.abrirVentana(this, controlPrincipal.VENTANA_VISTA_GRAFICO, param);
        }

        private void m3_Click(object sender, EventArgs e)
        {
            Close();
        }

        //private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        //{
        //    Graphics g = e.Graphics;
        //    //MessageBox.Show((this.Size.Width - 40).ToString() + ", " + (this.Size.Height - 150).ToString());
        //    //Rectangle r = new Rectangle(new Point(12, 12), new System.Drawing.Size(this.Size.Width - 40, this.Size.Height - 150));
        //    Rectangle r = new Rectangle(new Point(12, 12), new System.Drawing.Size(661, 410));
        //    estiloBarra.ChartArea = r;
        //    estiloBarra.SetChartArea(g);
        //    e.Graphics.TranslateTransform(PlotPanel.Left, PlotPanel.Top);
        //    AddData(g);
        //    coleccionDatos.AddBars(g, estiloBarra, coleccionDatos.DataSeriesList.Count, seriesDatos.PointList.Count);
        //    estiloBarra.PlotPanelStyle(g);
        //}


        public override void hacerVisible(object[] arg)
        {
            base.hacerVisible(arg);
            if (arg != null)
                inicializarGrafico(arg);
        }

        private int Max(LinkedList<datoEstadistico> lista)
        {
            int m = 0;
            foreach (datoEstadistico de in lista)
                if (de.cantidad >= m)
                    m = de.cantidad;
            return m;
        }

        private void inicializarGrafico(object[] argumentos)
        {
            SetStyle(ControlStyles.FixedWidth, true);
            datos = (LinkedList<datoEstadistico>)argumentos[3];

            if (datos != null && datos.Count > 0 && !datos.All(item => item.cantidad == 0))
            {
                PlotPanel.BorderStyle = BorderStyle.FixedSingle;
                estiloBarra.Title = (string)argumentos[0];
                estiloBarra.XLabel = (string)argumentos[1];
                estiloBarra.YLabel = (string)argumentos[2];

                estiloBarra.DateList = (LinkedList<datoEstadistico>)argumentos[3];

                estiloBarra.XLimMin = 0f;
                estiloBarra.XLimMax = (datos.Count > 0) ? datos.Count : 0;

                int m = Max(datos);
                estiloBarra.YLimMin = 0f;
                estiloBarra.XTick = 1f;

                if (m >= 0 && m <= 50)
                {
                    estiloBarra.YTick = 1f;
                }
                else if(m > 50 && m <= 500)
                {
                    estiloBarra.YTick = 10f;
                }
                else if (m > 500 && m <= 5000)
                {
                    estiloBarra.YTick = 100f;
                }
                else if (m > 5000 && m <= 50000)
                {
                    estiloBarra.YTick = 1000f;
                }
                else if (m > 50000 && m <= 500000)
                {
                    estiloBarra.YTick = 10000f;
                }
                else if (m > 500000 && m <= 5000000)
                {
                    estiloBarra.YTick = 100000f;
                }

                estiloBarra.YLimMax = (datos.Count > 0) ? ((m > 0) ? m + estiloBarra.YTick : 0) : 0;
                estiloBarra.BarType = ChartStyle.BarTypeEnum.Vertical;
            }
            else
            {
                PlotPanel.BorderStyle = BorderStyle.None;
                mostrarInfo("No existen resultados por mostrar.");
                Close();
            }
        }

        private void AddData(Graphics g)
        {
            float x, y;
            coleccionDatos.DataSeriesList.Clear();
            seriesDatos = new DataSeries();
            seriesDatos.BarStyle.BorderColor = Color.LightSteelBlue;
            seriesDatos.BarStyle.FillColor = Color.LightSlateGray;
            for (int i = 0; i < datos.Count; i++)
            {
                x = i + 1;
                y = datos.ElementAt<datoEstadistico>(i).cantidad;
                seriesDatos.AddPoint(new PointF(x, y));
            }
            coleccionDatos.Add(seriesDatos);
        }

        #endregion

        #region EVENTOS

        private void PlotPanel_Paint(object sender, PaintEventArgs e)
        {
            if (datos != null && datos.Count > 0 && !datos.All(item => item.cantidad == 0))
            {
                Graphics g = e.Graphics;
                AddData(g);
                coleccionDatos.AddBars(g, estiloBarra, coleccionDatos.DataSeriesList.Count, seriesDatos.PointList.Count);
                estiloBarra.PlotPanelStyle(g);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (datos != null && datos.Count > 0 && !datos.All(item => item.cantidad == 0))
            {
                Graphics g = e.Graphics;
                Rectangle r = new Rectangle(new Point(12, 12), new System.Drawing.Size(this.Size.Width - 40, this.Size.Height - 150));
                estiloBarra.ChartArea = r;
                estiloBarra.SetChartArea(g);
            }
        }

        private void frm_ReporteEstadistico_SizeChanged(object sender, EventArgs e)
        {
            PlotPanel.Invalidate(PlotPanel.Region);
            this.Invalidate(PlotPanel.Region);
        }

        #endregion

        #region ATRIBUTOS

        private ChartStyle estiloBarra;
        private DataCollection coleccionDatos;
        private DataSeries seriesDatos;
        private LinkedList<datoEstadistico> datos;

        private MainMenu mm;
        private MenuItem m1;
        private MenuItem m2;
        private MenuItem m3 = new MenuItem("Salir");

        #endregion
    }
}
